//
//  AddRainView.m
//  UI09_作业
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AddRainView.h"
#import "RainScrollView.h"
@interface AddRainView ()<UIScrollViewDelegate>
@property (nonatomic,retain)RainScrollView *rainScrollView;

@property (nonatomic,retain)UIPageControl *pageControl;
@end

@implementation AddRainView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createView];
        self.imageArray = [NSArray array];
    }
    return self;
}
- (void)createView{
    self.rainScrollView = [[RainScrollView alloc] initWithFrame:self.bounds];
    self.rainScrollView.delegate = self;
    //设置偏移量
    self.rainScrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);
    //添加图片
    [self addSubview:_rainScrollView];
    
    //创建pageControl,并添加到父视图上
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) / 5 * 3, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) / 5 * 2)];
    self.pageControl.pageIndicatorTintColor = [UIColor blackColor];

    [self addSubview:_pageControl];
    [_pageControl release];
    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
   
}
- (void)setImageArray:(NSArray<UIImage *> *)imageArray{
    if (_imageArray != imageArray) {
        [_imageArray release];
        _imageArray = [imageArray retain];
        [self.rainScrollView addPhoto:self.imageArray];
        self.pageControl.numberOfPages = self.imageArray.count - 2;
        NSLog(@"%@",self.imageArray);
        
    }
}

- (void)pageAction:(UIPageControl *)page{
    [self.rainScrollView setContentOffset:CGPointMake((page.currentPage + 1) * CGRectGetWidth(self.bounds), 0) animated:YES];
    [self.delegate passValue:self.pageControl.currentPage];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    for (UIScrollView *scroll in scrollView.subviews) {
        if ([scroll isKindOfClass:[UIScrollView class]]) {
            scroll.zoomScale = 1;
        }
    }
    if (scrollView.contentOffset.x == 0) {
        self.rainScrollView.contentOffset = CGPointMake((self.imageArray.count - 2) * CGRectGetWidth(self.bounds), 0);
    }
    if (scrollView.contentOffset.x == (self.imageArray.count - 1) * CGRectGetWidth(self.bounds)) {
        self.rainScrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);
    }
    
    
    self.pageControl.currentPage = self.rainScrollView.contentOffset.x / CGRectGetWidth(self.bounds) - 1;

    [self.delegate passValue:self.pageControl.currentPage];

}
@end
